package controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import pojo.User;
import pojo.Userlist;
import service.UserService;
import service.UserlistService;

@Controller
public class UserlistController {

	@Autowired
	private UserlistService userlistService;

	@Autowired
	private UserService userService;

	@RequestMapping("/findhasuserlist")
	public String findhasuserlist(HttpSession httpSession, Model model) throws Exception {
		User user1 = (User) httpSession.getAttribute("loginUser");
		Integer user_id = user1.getId();
		Userlist userlist = userlistService.findhasuserlist(user_id);
		model.addAttribute("userlist", userlist);
		model.addAttribute("mainPage", "updateuserlist.jsp");
		String type = user1.getType();
		if ("fangdong".equals(type)) {
			return "fangdong/main";
		} else {
			return "zuke/main";
		}
	}

	// 查找并更新用户信息
	@RequestMapping("/checkuserlist")
	public String checkuserlist(Model model, Userlist userlist, HttpSession httpSession) throws Exception {
		if (userlist.getId() == null) {
			String idcard = userlist.getIdcard();
			Userlist list = userlistService.checkuserlist(idcard);

			if (list != null) {
				model.addAttribute("error", "该身份证已被绑定,一个身份证号码只能被一个账户绑定！");
				model.addAttribute("mainPage", "updateuserlist.jsp");
				model.addAttribute("userlist", userlist);
			} else {
				User user1 = (User) httpSession.getAttribute("loginUser");
				Integer user_id = user1.getId();
				userlist.setUser_id(user_id);
				userlistService.insertuserlist(userlist);
				Userlist list1 = userlistService.checkuserlist(idcard);
				model.addAttribute("error", "资料完善成功");
				model.addAttribute("mainPage", "updateuserlist.jsp");
				model.addAttribute("userlist", list1);
			}

		} else {

			Userlist list = userlistService.finduserlistupdate(userlist);
			if (list != null) {
				model.addAttribute("error", "该身份证号码已被绑定");
				model.addAttribute("mainPage", "updateuserlist.jsp");
				model.addAttribute("userlist", userlist);
			} else {
				userlistService.updateuserlist(userlist);
				model.addAttribute("error", "更新成功");
				model.addAttribute("mainPage", "updateuserlist.jsp");
				model.addAttribute("userlist", userlist);
			}

		}
		return "zuke/main";
	}

	@RequestMapping("/findalluserlist")
	public String findalluserlist(Model model, @RequestParam(required = false, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "3") Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<Userlist> userlist = userlistService.findalluserlist();
		PageInfo<Userlist> p = new PageInfo<Userlist>(userlist);
		model.addAttribute("userlist", userlist);
		model.addAttribute("p", p);
		model.addAttribute("mainPage", "userlist.jsp");
		return "admin/main";
	}

	// 删除用户信息
	@RequestMapping("/deleteuserlist")
	public String deleteuserlist(Model model, Integer id) {
		userlistService.deleteuserlist(id);
		model.addAttribute("error", "deletesuccess");
		return "redirect:findalluserlist.action";
	}

	@RequestMapping("/toadduser")
	public String toAddUser(HttpSession httpSession, Model model) {
		model.addAttribute("mainPage", "useradd.jsp");
		return "admin/main";
	}

	@RequestMapping("/toUpdateuser")
	public String toUpdateuser(Integer id, HttpSession httpSession, Model model) {
		Userlist userlist = userlistService.findhasuserlist(id);
		model.addAttribute("userlist", userlist);
		model.addAttribute("mainPage", "userupdate.jsp");
		return "admin/main";
	}

	@RequestMapping("/updateuser")
	public String updateuser(Userlist userlist, String username, Integer userid) {
		userlistService.updateuser(userlist, username, userid);
		return "redirect:findalluserlist.action";
	}

	@RequestMapping("/adduser")
	public String addUser(Userlist userlist, User user, Model model) throws Exception {
		userService.insert(user);
		userlist.setUser_id(user.getId());
		userlistService.insertuserlist(userlist);
		model.addAttribute("error", "添加成功");
		return "redirect:findalluserlist.action";
	}
}
